package com.paic.mhis.hcpms.monitor.biz.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.paic.mhis.core.user.dto.AreaInfoDTO;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.Constants;
import com.paic.mhis.hcpms.monitor.biz.service.MonitorIntegrityService;
import com.paic.mhis.hcpms.monitor.dto.MonitorIntegrityMapDTO;
import com.paic.mhis.hcpms.monitor.intergration.dao.MonitorIntegrityDAO;

/**
 * 诚信监控 service 接口实现类
 * 
 * @author EX-XIEQI001
 * 
 */
@Service("monitorIntegrityService")
public class MonitorIntegrityServiceImpl implements MonitorIntegrityService {

	@Autowired
	public MonitorIntegrityDAO monitorIntegrityDao;

	/**
	 * 获取查询数据的时间维度
	 * 
	 * @return
	 */
	private List<String> getQueryTimeRang(String startDate, String endDate) {

		String[] startDates = startDate.split("-");
		String[] endDates = endDate.split("-");

		List<String> list = new ArrayList<String>();

		int startMonth = Integer.parseInt(startDates[1]), endMonth = Integer.parseInt(endDates[1]), startYear = Integer
				.parseInt(startDates[0]), endYear = Integer.parseInt(endDates[0]);
		int length = endMonth + ((endYear - startYear) > 0 ? (13 - startMonth) : 0);
		for (int m = 0; m < length; m++) {
			if (startMonth > 12) {
				startYear = startYear + 1;
				startMonth = 1;
			}
			if (startMonth < 10) {
				list.add(startYear + "-0" + startMonth);
			} else {
				list.add(startYear + "-" + startMonth);
			}
			startMonth++;
		}
		return list;
	}

	@Override
	public Map<String, Object> queryMonitorIntegrity(Map<String, Object> map, List<String> orgLevels) {
		Map<String, Object> result = new HashMap<String, Object>();
		
		if  (null == orgLevels) {
			orgLevels = new ArrayList<String>();
		}
		
		// 机构类型
		if (null != orgLevels && orgLevels.size() > 0) {
			map.put("orgLevels", orgLevels);
		}
		// 总数
		result.put("countNum", monitorIntegrityDao.queryCountNum(map));

		List<String> listDates = getQueryTimeRang(map.get("startDate").toString(), map.get("endDate").toString());

		List<Integer> listNums = new ArrayList<Integer>();
		List<String> datas = new ArrayList<String>();
		for (String date : listDates) {
			map.put("happenDate", date);
			int num = monitorIntegrityDao.queryMonthCountNum(map);
			listNums.add(num);
			datas.add(date.substring(2));
		}

		// 日期
		result.put("data", datas);

		// 日期对应的值
		result.put("caseData", listNums);
		List<Map<String, Object>> orgDatas = new ArrayList<Map<String, Object>>();
		for (String orgLeve : orgLevels) {
			map.put("orgLevel", orgLeve);
			Map<String, Object> orgMap = new HashMap<String, Object>();
			orgMap.put("name", orgLeve);
			orgMap.put("value", monitorIntegrityDao.queryOrgLevelCount(map));

			orgDatas.add(orgMap);
		}

		result.put("levelData", orgDatas);
		return result;
	}

	@Override
	public Map<String, Object> queryKeyMonitoring() {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("insuredDatas", monitorIntegrityDao.queryPatientCount());
		result.put("staffDatas", monitorIntegrityDao.queryInsuranceScore());

		return result;
	}

	@Override
	public Map<String, Object> queryMonitorIntegrityMap(Map<String, Object> map, String userId) {

		Map<String, Object> result = new HashMap<String, Object>();
		// 获取行政区域集合
		List<AreaInfoDTO> areaDatas = this.monitorIntegrityDao.queryAreaAll(userId);
		if (null != areaDatas && areaDatas.size() > 0) {
			// 获取上级行政区域的编码
			AreaInfoDTO dto = areaDatas.get(0);

			map.put("parentCode", dto.getParentCode());
			List<MonitorIntegrityMapDTO> mapDtos = this.monitorIntegrityDao.queryAreaOrgLevelCount(map);
			result.put("mapDtos", mapDtos);
			result.put("areaDatas", areaDatas);
			result.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
		} else {
			result.put(Constants.STATE_KEY, Constants.FAILED_VALIDATE_STATE_VALUE);
			result.put(Constants.MSG_KEY, "权限不足,请联系管理员");
		}

		return result;
	}

}
